<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AgricGoodsSeller;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class AgricGoodsSellerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AgricGoodsSeller(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('seller_name');
            $grid->column('seller_num');
            $grid->column('head_portrait')->image('', 60, 60);
            $grid->column('wx');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AgricGoodsSeller(), function (Show $show) {
            $show->field('id');
            $show->field('seller_name');
            $show->field('seller_num');
            $show->field('head_portrait')->image();
            $show->field('wx');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AgricGoodsSeller(), function (Form $form) {
            $form->display('id');
            $form->text('seller_name');
            $form->mobile('seller_num');
            $form->image('head_portrait')->move('goods_seller/'.date('Y-m-d'))->autoUpload();
            $form->text('wx');
        
            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function ($form) {
                // 商品名验证
                $query = \App\Models\AgricGoodsSeller::query();
                $query->where('seller_name', $form->seller_name);
                $query->where('seller_num', $form->seller_num);
                if ($form->model()->id) {
                    $query->where('id', '!=', $form->model()->id);
                }
                $checkName = $query->first();
                if ($checkName) {
                    $form->responseValidationMessages('seller_name', '卖家已存在');
                }
            });
        });
    }
}
